package cn.dujc.superkid.activities;


import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;

import cn.dujc.core.adapter.BaseQuickAdapter;
import cn.dujc.core.ui.BaseListActivity;
import cn.dujc.core.util.ToastUtil;
import cn.dujc.superkid.adapter.MyExamAdapter;
import cn.dujc.superkid.api.HttpHelper;
import cn.dujc.superkid.api.QMap;
import cn.dujc.superkid.api.Result;
import cn.dujc.superkid.api.ResultCallBack;
import cn.dujc.superkid.bean.MyExamineResult;
import cn.dujc.superkid.bean.RegisterResult;
import cn.dujc.superkid.configs.Globals;
import cn.dujc.superkid.utils.Preferences;

// TODO: 2018/8/15 0015 上拉加载没有效果；
public class MyExamActivity extends BaseListActivity {
    private int mPage = 0;
    private MyExamAdapter myExamAdapter;
    private final List<MyExamineResult.MyExamineEntity> list = new ArrayList<>();

    @Override
    public void initBasic(Bundle savedInstanceState) {
        super.initBasic(savedInstanceState);
        setTitle("我的考试");
        getSwipeRefreshLayout().setEnabled(false);//禁止下拉刷新
    }

    @Nullable
    @Override
    protected BaseQuickAdapter initAdapter() {
        myExamAdapter = new MyExamAdapter(list);
        return myExamAdapter;
    }

    protected void recyclerViewOtherSetup() {
        if (mRvList != null) {
            //去掉下划线
//            mRvList.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
//            myQuestionAdapter.disableLoadMoreIfNotFullPage();//禁止加载
        }
    }

    @Override
    protected void onItemClick(int position) {
        starter()
                .with(MyTestResultsActivity.EXTRA_MYTEST, myExamAdapter.getItem(position))
                .go(MyTestResultsActivity.class);
    }

    @Override
    protected void loadMore() {
        loadData();
    }

    @Override
    protected void reload() {
        showRefreshing();
        mPage = 0;
        loadData();
    }

    private void loadData() {
        if (!Preferences.isLogin(this)) {
            starter().go(LoginActivity.class);
        }
        final RegisterResult loginResult = Preferences.getInstance().getLoginResult(mActivity);
        HttpHelper.getUserCenter().memberMyExamine(QMap.build()
                .put("user_id", loginResult.getUid())
                .put("channel",  Preferences.getInstance().getChannel(mActivity).getChannel_id())
                .put("logincode", loginResult.getLogincode())
                .put("type_id","1")
                .put("num", Globals.PAGE_SIZE)
                .put("page", mPage)
                .create()).enqueue(new ResultCallBack<Result<MyExamineResult>>(mActivity) {
            @Override
            public void onSuccessCallback(int code, Result<MyExamineResult> body) {
//                ToastUtil.showToast(mActivity,mPage);
                if (mPage <= 0) list.clear();
                boolean done = false;
                final MyExamineResult data = body.getData();
                if (data != null) {
                    final List<MyExamineResult.MyExamineEntity> data1 = data.getList();
                    if (data1 != null) {
                        MyExamActivity.this.list.addAll(data1);
                    }
                    done = mPage >= data.getPage_num();
                }
                Handler handler = new Handler();
                final boolean finalDone = done;
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        notifyDataSetChanged(finalDone);
                        mPage++;
                    }
                }, 500);
            }
            @Override
            public void onFailureCallback(int code, String msg) {
                super.onFailureCallback(code, msg);
                loadFailure();
                loadDone(true,false);
            }
        });
    }
}
